function Login() {
	new MyApp.view.Login();
}

function Regist() {
	new MyApp.view.Regist();
}

Ext.define('MyApp.view.Regist', {
    extend: 'Ext.window.Window',

    frame: true,
    height: 316,
    hidden: false,
    id: 'Regist',
    renderTo: 'container',
    width: 407,
    layout: {
        type: 'absolute'
    },
    title: '注册',
    modal: true,

    initComponent: function() {
        var me = this;

        Ext.applyIf(me, {
            items: [
                {
                    xtype: 'textfield',
                    x: 50,
                    y: 30,
                    id: 'username',
                    width: 290,
                    fieldLabel: '用户名',
                    name: 'username'
                },
                {
                    xtype: 'textfield',
                    x: 50,
                    y: 70,
                    id: 'password',
                    width: 290,
                    fieldLabel: '密码',
                    name: 'password',
                    inputType: 'password'
                },
                {
                    xtype: 'textfield',
                    x: 50,
                    y: 110,
                    id: 'password2',
                    width: 290,
                    fieldLabel: '确认密码',
                    name: 'password2',
                    inputType: 'password'
                },
                {
                    xtype: 'button',
                    handler: function(button, event) {
                        if(Ext.getCmp('password').getValue()!=Ext.getCmp('password2').getValue()){
                            alert('两次输入的密码不一致！');
                        }
                        else{
                            Ext.Ajax.request({
                                url: './registAction',
                                params:{
                                    username:Ext.getCmp('username').getValue(),
                                    password:Ext.getCmp('password').getValue(),
                                    password2:Ext.getCmp('password2').getValue(),
                                    userclass:Ext.getCmp('uc').getValue()
                                },
                                success: function(response, opts) {
                                    if(Ext.getCmp('uc').getValue()=='用户')
                                    window.location.href="./userView.jsp";
                                    else
                                    window.location.href="./managerView.jsp";
                                },
                               failure: function(response, opts) {
                               		alert('用户名已存在！');
                               	}

                            });
                        }



                    },
                    x: 100,
                    y: 200,
                    height: 30,
                    id: 'ok',
                    width: 70,
                    text: '确认'
                },
                {
                    xtype: 'button',
                    handler: function(button, event) {
                        Ext.getCmp("Regist").close();
                    },
                    x: 220,
                    y: 200,
                    height: 30,
                    id: 'cancel',
                    width: 70,
                    text: '取消'
                },
                {
                    xtype: 'splitbutton',
                    x: 50,
                    y: 150,
                    height: 30,
                    id: 'userclassbtn',
                    width: 290,
                    text: '请选择用户类型',
                    menu: {
                        xtype: 'menu',
                        id: 'userclass',
                        items: [
                            {
                                xtype: 'menuitem',
                                handler: function(item, e) {
                                    Ext.getCmp('userclassbtn').setText('管理员');
                                    Ext.getCmp('uc').setValue('管理员');
                                },
                                text: '管理员'
                            },
                            {
                                xtype: 'menuitem',
                                handler: function(item, e) {
                                    Ext.getCmp('userclassbtn').setText('用户');
                                    Ext.getCmp('uc').setValue('用户');
                                },
                                text: '用户'
                            }
                        ]
                    }
                },
                {
                    xtype: 'textfield',
                    x: 130,
                    y: 250,
                    hidden: true,
                    id: 'uc',
                    fieldLabel: '',
                    name: 'userclass'
                }
            ]
        });

        me.callParent(arguments);
    }

});


Ext.define('MyApp.view.Login', {
    extend: 'Ext.window.Window',

    frame: true,
    height: 257,
    hidden: false,
    id: 'Login',
    renderTo: 'container',
    width: 405,
    title: '登陆',
    modal: true,

    initComponent: function() {
        var me = this;

        Ext.applyIf(me, {
            items: [
                {
                    xtype: 'form',
                    border: false,
                    draggable: true,
                    frame: true,
                    height: 253,
                    id: 'LoginForm',
                    width: 402,
                    layout: {
                        type: 'absolute'
                    },
                    closable: true,
                    header: false,
                    simpleDrag: true,
                    title: 'LoginForm',
                    items: [
                        {
                            xtype: 'textfield',
                            x: 50,
                            y: 30,
                            id: 'username',
                            width: 290,
                            fieldLabel: '用户名',
                            name: 'username'
                        },
                        {
                            xtype: 'textfield',
                            x: 50,
                            y: 70,
                            id: 'password',
                            width: 290,
                            fieldLabel: '密码',
                            name: 'password',
                            inputType: 'password'
                        },
                        {
                            xtype: 'button',
                            handler: function(button, event) {
                                Ext.Ajax.request({
                                    url: './loginAction',
                                    params:{
                                        username:Ext.getCmp('username').getValue(),
                                        password:Ext.getCmp('password').getValue(),
                                        userclass:Ext.getCmp('uc').getValue()
                                    },
                                    success: function(response, opts) {
                                        if(Ext.getCmp('uc').getValue()=='用户')
                                        window.location.href="./userView.jsp";
                                        else
                                        window.location.href="./managerView.jsp";
                                    },
                                    failure: function(response, opts) {
                                        alert('用户名或密码错误！');
                                    }
                                });
                            },
                            x: 100,
                            y: 160,
                            height: 30,
                            id: 'ok',
                            width: 70,
                            text: '确认'
                        },
                        {
                            xtype: 'button',
                            handler: function(button, event) {
                                Ext.getCmp("Login").close();
                            },
                            x: 220,
                            y: 160,
                            height: 30,
                            id: 'cancel',
                            width: 70,
                            text: '取消'
                        },
                        {
                            xtype: 'splitbutton',
                            x: 50,
                            y: 110,
                            formBind: false,
                            height: 30,
                            id: 'userclassbtn',
                            width: 290,
                            text: '请选择用户类型',
                            menu: {
                                xtype: 'menu',
                                id: 'userclass',
                                items: [
                                    {
                                        xtype: 'menuitem',
                                        handler: function(item, e) {
                                            Ext.getCmp('userclassbtn').setText('管理员');
                                            Ext.getCmp('uc').setValue('管理员');
                                        },
                                        text: '管理员'
                                    },
                                    {
                                        xtype: 'menuitem',
                                        handler: function(item, e) {
                                            Ext.getCmp('userclassbtn').setText('用户');
                                            Ext.getCmp('uc').setValue('用户');
                                        },
                                        text: '用户'
                                    }
                                ]
                            }
                        },
                        {
                            xtype: 'textfield',
                            x: 130,
                            y: 200,
                            hidden: true,
                            id: 'uc',
                            fieldLabel: '',
                            name: 'userclass'
                        }
                    ]
                }
            ]
        });

        me.callParent(arguments);
    }

});